package gnacos

import "github.com/nacos-group/nacos-sdk-go/common/constant"

var clientConfig *constant.ClientConfig

var serverConfigs []constant.ServerConfig

type Addr struct {
	Ip   string
	Port uint64
}

type config struct {
	NamespaceId string //共用的namespace
	NacosPath   string //本地缓存位置
	LogLevel    string //日志级别
	addrs       []Addr //nacos服务端地址
	Scheme      string
}

type OptFunc func(*config)

func SetNacosPath(nacosPath string) OptFunc {
	return func(entry *config) {
		entry.NacosPath = nacosPath
	}
}

func SetLogLevel(logLevel string) OptFunc {
	return func(entry *config) {
		entry.LogLevel = logLevel
	}
}

func SetScheme(scheme string) OptFunc {
	return func(entry *config) {
		entry.Scheme = scheme
	}
}

//配置信息
func initConfig(namespace string, addrs []Addr, options ...OptFunc) {
	if clientConfig != nil && len(serverConfigs) > 0 {
		return
	}
	currentConfig := config{
		NacosPath: "nacos", //本地缓存位置
		LogLevel:  "info",  //日志级别
		Scheme:    "http",
	}

	for _, opt := range options {
		opt(&currentConfig)
	}

	clientConfig = &constant.ClientConfig{
		NamespaceId:         namespace,
		NotLoadCacheAtStart: true,
		CacheDir:            currentConfig.NacosPath + "/cache",
		LogDir:              currentConfig.NacosPath + "/log",
		RotateTime:          "24h",
		MaxAge:              5,
		LogLevel:            currentConfig.LogLevel,
	}

	for _, addr := range addrs {
		serverConfigs = append(serverConfigs, constant.ServerConfig{
			IpAddr: addr.Ip,
			Port:   addr.Port,
			Scheme: currentConfig.Scheme,
		})
	}

}
